package com.tree.thrive.business.input.domain.req;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * learning
 *
 * @Auther: cheng.tang
 * @Date: 2022/4/27 5:48 AM
 * @Description:
 */
@Slf4j
@Schema(description = "输入的参数。note的长度不能大于10", title = "请求参数")
@Data
public class InputCheckReq {

    @Schema(title = "备注", description = "如果超过10字符，后面的不要的", example = "1234567890超过10们了")
    private String note;

    @Schema(title = "name", example = "一棵大树")
    private String name;

    /**
     * 处理note的长度限制
     *
     * @param lengthLimit
     */
    public void subStringNote(int lengthLimit) {
        if (StringUtils.isBlank(note)) {
            setNote("");
            return;
        }
        if (note.length() > lengthLimit) {
            String newNote = note.substring(0, lengthLimit);
            log.warn("长度超了 name {} old {} newNote {}", name, note, newNote);
            setNote(newNote);
        }
    }

    public String buildLockKey() {
        List<String> keyParams = new ArrayList<>(2);
        if (StringUtils.isNotBlank(note)) {
            keyParams.add(note);
        }
        if (StringUtils.isNotBlank(name)) {
            keyParams.add(name);
        }
        return String.join(":", keyParams);
    }

}
